package cs5300;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import BasicDataType.CookieType;

/**
 * Servlet implementation class logout
 */
@WebServlet("/Logout")
public class Logout extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public Logout() {
		super();
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// out put the BYE~!
		PrintWriter writer = new PrintWriter(response.getWriter());
		response.setContentType("text/html");
		writer.println("Bye<br>");
		// delete the Session in the hash table
		Cookie cookies[] = request.getCookies();
		System.out.println(cookies.length);
		for (int i = 0; i < cookies.length; i++) {
			if (cookies[i].getName().equals(SessionManager.CookieName)) {
				CookieType cookieT = new CookieType(cookies[i].getValue());
				SessionManager sManager = SessionManager.getInstance();
				sManager.rpcclient.sessionDelete(cookieT);
				sManager.sTable.removeState(cookieT.sessionID);
				SessionManager.sTable.removeState(cookieT.sessionID);
			}
		}
	}

}
